package com.dinian.zeus.util;

import com.dinian.zeus.domain.Newspaper;
import com.dinian.zeus.util.annotation.column;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: dinian
 * @Date: 2018/11/17
 */
@Slf4j
public class ExcelUtil {

    private static boolean isExcel2007(String filePath){
        return filePath.matches("^.+\\.(?i)(xlsx)$");
    }

    /**
     * 导入数据报刊
     * @param filePath  上传的文件
     * @param startRow  从excel的第几行开始读数据
     * @param <T>
     * @return
     * @throws IOException
     */
    public static <T> List<T> importData(String filePath,Class c,Integer startRow) throws Exception {

        Workbook workbook = null;
        InputStream inputStream = new FileInputStream(filePath);
        List<T> arrayList = new ArrayList();
        try {
            if(isExcel2007(filePath)){
                workbook = new XSSFWorkbook(inputStream);
            }else {
                workbook = new HSSFWorkbook(inputStream);
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            inputStream.close();
        }
        Sheet sheet = workbook.getSheetAt(0);//获取第一张表
        checkTitle:
        for (Row row : sheet){
            if (row.getRowNum() < startRow-1){
                continue;
            }
            try {
                Class<?> clz = Class.forName(c.getName());
                T object = (T)clz.newInstance();
                Field[] fields = clz.getDeclaredFields();
                int i=0;
                for(Field field:fields){
                    if ("id".equals(field.getName()) || "createTime".equals(field.getName()) || "updateTime".equals(field.getName())) continue;
                    row.getCell(i).setCellType(CellType.STRING);
                    String stringCellValue = row.getCell(i).getStringCellValue(); //excel列标题
                    String value = field.getAnnotation(column.class).value();     //newspaper 字段注解名
                    if (row.getRowNum() == startRow-1 && !value.equals(stringCellValue)) break checkTitle;
                    if (row.getRowNum() == startRow-1 && value.equals(stringCellValue)) continue checkTitle;
                    field.setAccessible(true);
                    field.set(object,stringCellValue);
                    i++;
                }
                arrayList.add(object);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        log.info("total row : {}",arrayList.size());
        return arrayList;
    }

    /**
     * 导出数据
     * @param newspaperList
     * @param templetFilePath
     * @param exportFilePath
     */
    public static void exportExcel(List<Newspaper> newspaperList, String templetFilePath, String exportFilePath){
        Workbook workbook;
        try {
            FileOutputStream out = new FileOutputStream(exportFilePath);
            if(ExcelUtil.isExcel2007(templetFilePath)){
                workbook = new XSSFWorkbook();
            }else {
                workbook = new HSSFWorkbook();
            }
            Sheet sheet = workbook.createSheet("sheet");
            int rowIndex = 0;
            for (Newspaper newspaper:newspaperList){
                Row row=sheet.createRow(rowIndex);
                row.createCell(1).setCellValue(newspaper.getNewsName());
                rowIndex++;
            }
            workbook.write(out);
            out.flush();
            out.close();
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
